package com.slwish.xty.ui.activity;

import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.slwish.common.base.PresenterToolbarActivity;
import com.slwish.common.interf.PerfectClickListener;
import com.slwish.common.utils.IntentUtils;
import com.slwish.xty.R;
import com.slwish.xty.entity.UserModel;
import com.slwish.xty.mvp.contract.LoginContract;
import com.slwish.xty.mvp.presenter.LoginPresenter;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.OnClick;

public class LoginActivity extends PresenterToolbarActivity<LoginContract.Presenter> implements LoginContract.View {

    @BindView(R.id.et_username)
    EditText etUserName;
    @BindView(R.id.et_password)
    EditText etPassword;
    @BindView(R.id.btn_login)
    TextView btnLogin;

    @Override
    protected int getLayoutId() {
        return R.layout.act_login;
    }

    @Override
    protected LoginContract.Presenter initPresenter() {
        return new LoginPresenter(this);
    }

    @Override
    protected void initWidget() {
        super.initWidget();
        setTitle(R.string.login_text);
        setRightText(R.string.register_text);
        btnLogin.setEnabled(false);
        etUserName.addTextChangedListener(textWatcher);
        etPassword.addTextChangedListener(textWatcher);
    }

    @Override
    protected void initEvent() {
        super.initEvent();
        EventBus.getDefault().post("closeDrawer");
        setRightOnClickListener(new PerfectClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                IntentUtils.startActivity(mContext, RegisterActivity.class);
            }
        });
    }

    //输入监听
    TextWatcher textWatcher = new TextWatcher() {
        @Override
        public void onTextChanged(CharSequence s, int start, int before,
                                  int count) {
            if (TextUtils.isEmpty(etPassword.getText()) || TextUtils.isEmpty(etUserName.getText())) {
                btnLogin.setBackgroundResource(R.drawable.btn_unable_selector);
                btnLogin.setEnabled(false);
            } else {
                btnLogin.setBackgroundResource(R.drawable.btn_select_selector);
                btnLogin.setEnabled(true);
            }
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                                      int after) {
        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    };

    @OnClick({R.id.tv_forget_pass, R.id.btn_login})
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_login://登录
                mPresenter.login(mContext, etUserName.getText().toString(), etPassword.getText().toString());
                break;
            case R.id.tv_forget_pass://忘记密码
                IntentUtils.startActivity(mContext, ForgetPsdActivity.class);
                break;
        }
    }

    /**
     * 登录成功
     *
     * @param moldel
     */
    @Override
    public void loginSuccess(UserModel moldel) {
        EventBus.getDefault().post(moldel);
        finish();

    }

}
